package com.peng.test.jdk;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * MathSample
 *
 * @author lupeng
 * Created on 2022-06-16
 */
public class MathSample {

    private static final Logger logger = LoggerFactory.getLogger(MathSample.class);

    @Test
    public void s1() {
        long a = 12345;
        BigDecimal ba = new BigDecimal(a);
        BigDecimal bb = new BigDecimal(100);
        System.out.println(bb.divide(ba).doubleValue());
    }


    @Test
    public void s2() {
        double i = new BigDecimal(12345).divide(new BigDecimal(1000)).doubleValue();
        System.out.println(i);
        System.out.println(new DecimalFormat("#.##").format(i));
        System.out.println(new DecimalFormat("#.#").format(i));
        System.out.println("ceil: " + Math.ceil(i));
        System.out.println("floor: " + Math.floor(i));
        System.out.println("round: " + Math.round(i));

        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setGroupingUsed(false);
        nf.setRoundingMode(RoundingMode.DOWN);

        nf.setMaximumFractionDigits(2);
        System.out.println(nf.format(i));

        nf.setMaximumFractionDigits(1);
        System.out.println(nf.format(i));

        nf.setMaximumFractionDigits(0);
        System.out.println(nf.format(i));
        System.out.println(nf.format(100000));
    }

    @Test
    public void s3() {
        double i = new BigDecimal(4).divide(new BigDecimal(100)).doubleValue();
        System.out.println(i);

        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setGroupingUsed(false);
        nf.setRoundingMode(RoundingMode.DOWN);

        nf.setMaximumFractionDigits(2);
        System.out.println(nf.format(i));
    }
}
